package mk.com.ldm.corepoint.implementation.client.presenter;


import java.util.Date;

import mk.com.ldm.corepoint.implementation.client.CoreServiceAsync;
import mk.com.ldm.corepoint.implementation.shared.LoginData;

import org.gwt.mosaic.ui.client.InfoPanel;
import org.gwt.mosaic.ui.client.MessageBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
/**
 * Presenter �� Login View. ��������� �� Login events (��������)
 * � ������������ �� �������� ���������.
 *
 */
public class LoginPresenter implements Presenter {  

  public interface Display {
    Button getLoginButton();
    TextBox getUsername();
    PasswordTextBox getPassword();
    FlexTable getContentTable();
    Widget asWidget();
  }
  
  private final CoreServiceAsync rpcService;
  private final HandlerManager eventBus;
  private final Display display;
  final String userCookie = Cookies.getCookie("username");
  final String hashCookie = Cookies.getCookie("hash");
  final String userLd = LoginData.getInstance().getUsername();
  final String hashLd = LoginData.getInstance().getHash();
  
  public LoginPresenter(CoreServiceAsync rpcService, HandlerManager eventBus, Display view) {
    this.rpcService = rpcService;
    this.eventBus = eventBus;
    this.display = view;
  }
  
  public void bind() {
	  display.getLoginButton().addClickHandler(new ClickHandler(){

		@Override
		public void onClick(ClickEvent event) {
			String username = display.getUsername().getValue();
			String password = display.getPassword().getValue();
			doLogin(username,password);
		}
		  
	  });
	  
	  display.getPassword().addKeyDownHandler(new KeyDownHandler() {

		@Override
		public void onKeyDown(KeyDownEvent event) {
			if (event.getNativeKeyCode() == 13) {
				String username = display.getUsername().getValue();
				String password = display.getPassword().getValue();
				doLogin(username,password);
		    }
		}
		  
	  });
		  
  }
  
  
  public void go(final HasWidgets container) {
    bind();
    container.clear();
    verifyLogin(userCookie, hashCookie, container);
}
/**
   * Rpc call �� �������� �� ��������. �� ��������� ����������� �� LoginData [singleton], �
   * ��������� cookies.
   */
  
  public void doLogin(String username, String password){
	  final String user = username;
	  
	  Date expires = new Date();
		long now = expires.getTime();
//		now = now + (1000 * 60 * 60 * 2);// two hours
		now = now + (1000 * 60 * 60 * 24 * 7);//seven days
//		now = now + (1000 * 60 * 1); //minute
		expires.setTime(now);
		Cookies.setCookie("username", user, expires);
		Cookies.setCookie("hash", "1a1dc91c907325c69271ddf0c944bc72", expires);
		LoginData.getInstance().setUsername(user);
		LoginData.getInstance().setHash("1a1dc91c907325c69271ddf0c944bc72");
		Window.Location.reload();
	  
//	  rpcService.login(username, password, new AsyncCallback<String> (){
//		 
//		@Override
//		public void onSuccess(String result) {
//			if(result.equals("")){
//				InfoPanel.show("Server", "Incorrect username or password. Try again.");
//			} 
//			else {
//				Date expires = new Date();
//				long now = expires.getTime();
////				now = now + (1000 * 60 * 60 * 2);// two hours
////				now = now + (1000 * 60 * 60 * 24 * 7);//seven days
//				now = now + (1000 * 60 * 1); //minute
//				expires.setTime(now);
//				Cookies.setCookie("username", user, expires);
//				Cookies.setCookie("hash", result, expires);
//				LoginData.getInstance().setUsername(user);
//				LoginData.getInstance().setHash(result);
//				Window.Location.reload();
//			}
//			
//		}  
//		  
//		@Override
//		public void onFailure(Throwable caught) {
//			MessageBox.error("Server", "Failed to communicate with the server");
//		}
//
//		
//		  
//	  });
	  
  }
  
  /**
   * ���������� ��������� ����� ����������, �������� GUI ��� ���� �� ���������� 
   */
  public void verifyLogin(String username, String hash, final HasWidgets container){
	  
	  if(userLd == null && hashLd == null && userCookie != null && hashCookie != null){
		  rpcService.loginCheck(username, hash, new AsyncCallback<Boolean>(){

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.error("Server", "Failed to communicate with the server");
				}

				@Override
				public void onSuccess(Boolean result) {
//					if(result == true){
						LoginData.getInstance().setUsername(userCookie);
						LoginData.getInstance().setHash(hashCookie);
						History.newItem("Transformer/WorkFlow");
//					}
//					else {
//						LoginData.getInstance().setUsername(null);
//					  	LoginData.getInstance().setHash(null);
//					  	Cookies.removeCookie("username");
//					  	Cookies.removeCookie("hash");
//					  	History.newItem("login");
//					  	Window.Location.reload();
//					}
				}
				  
		  });
	   } else if(userLd != null && hashLd != null){
				History.back();
	   } else {
		   	container.add(display.asWidget());
	   }
  }



}
